
var currentSelected = null;
var currentSelectedId = null;
var lastSelectedFileId = null;
var fileName;

function Load()
{
	try
	{
		document.all["openButton"].value = tabResources[2];
		document.all["cancelButton"].value = tabResources[1];
		
		//window.document.all["treeControl1treeSize"].style.backgroundColor = "#ffffff";
		
		setTimeout("RealLoad()", 300);	
	}
	catch(e)
	{
		//alert("load() popup.apsx : " + e.err);
	}
}

function RealLoad()
{
	try
	{
		var Args;
		var tmp;
		var tabFilter;
		var tabSoft;
		var exts;
		var appName;
		var html;
		
		Args = window.dialogArguments;
		window.returnValue = Args.directory;
		
		tmp = Args.directory.split("\\");
		tmp = tmp[tmp.length-1];
		if(tmp.indexOf(".") != -1)
		{
			fileName = tmp;
		}
		else
		{
			fileName = "";
		}
		
		//construction dropDown des extensions
		tabFilter = Args.filter.split(':');
		html = '<select name="choiceExts" style="width:210;" onchange="ChangeFilterSelection()">';
		for(i=0; i<tabFilter.length; i++)
		{
			tabSoft = tabFilter[i].split(',');
			appName = tabSoft[0];
			exts = "";
			for(j=1; j<tabSoft.length; j++)
			{
				exts += tabSoft[j];
				if(j<tabSoft.length-1)
				{
					exts += ",";
				}
			}
			//alert(exts);
			html += '<option value="' + exts + '" class="fileItem">' + appName + ' {' + exts + '}</option>'; 
		}
		html += '</select>';
		document.all["filterChoice"].innerHTML = html;
		
		setTimeout("whenReady()", 300);	
	}
	catch(e)
	{}
}

function whenReady()
{
	try
	{
		var Args;
		var tmp;
		var folder;
		var tabFolder;
		var tabMem;
		
		var k;
		var i;
		
		Args = window.dialogArguments;
		folder = Args.directory.replace(Args.defaultFolder, "");
		tmp = folder.substring(folder.length-1, folder.length);
		if(tmp == "\\")
		{
			folder = folder.substring(folder.length-1, folder.length);
		}
		//alert(folder);
		
		tabFolder = folder.split("\\");
		tmp = tabFolder[tabFolder.length-1];
		k=1;
		if(tmp.indexOf(".") != -1)
		{
			RealSelectFolder(Args.defaultFolder + folder.replace(tmp, ""));
			//document.all["fileSelected"].value = tmp;
			FileItemClick(tmp, lastSelectedFileId);
			
			k = 0;
		}
		
		/*tabMem = new Array();
		tabMem[0] = Args.defaultFolder;
		for(i=1; i<tabFolder.length+k && tabFolder != ""; i++)
		{
			tmp = Args.defaultFolder;
			for(j=0; j<i; j++)
			{
				tmp += tabFolder[j] + "\\";
			}
			tabMem[i] = tmp;
			
		}
		//alert(tabMem);
		window.document.all["treeControl1rootSel"].tree.root.tabMemFolders = tabMem;
		
		//open();
		
		document.all["fileSelected"].value = fileName;*/
	}
	catch(e)
	{
		//alert("whenReady() popup.apsx : " + e.err);
	}
}

function ReturnSelectedFile()
{
	try
	{
		var Args;
		
		Args = window.dialogArguments;
		if(document.all["fileSelected"].value != "")
		{
			window.returnValue = currentSelected + document.all["fileSelected"].value;
			window.close();
		}
		else
		{
			//alert(tabResources[5]);
		}
	}
	catch(e)
	{}
}

function ChangeFilterSelection()
{
	/*if(currentSelectedId != null)
	{
		ClickOnFolder(currentSelectedId);
	}*/
	if(currentSelected != null)
	{
		RealSelectFolder(currentSelected);
	}
}

function RealSelectFolder(directory)
{
	try
	{
		//alert(directory);
		var Args;
		var tabSelected;
		var xmlHttp = null;
		
		var post;
		var response;
		var responseDocument;
		var doc;
		var tabDoc;
		var html;
				
		Args = window.dialogArguments;

		document.all["fileSelected"].value = "";
		
		//tabSelected = selected.split(',');
		//directory = tabSelected[tabSelected.length-1];
		
		//currentSelectedId = selected;
		currentSelected = directory;
		
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp = null;
		}
		post = "directory=" + directory + "&filter=" + document.all["choiceExts"].value;
		//alert(post);
		xmlHttp.Open("POST", Args.webServiceUrl + '/GetFilesFromFolder', false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.Send(post);
		
		doc = xmlHttp.responseXML.text;
			
		doc = doc.replace("<![CDATA[", "");
		doc = doc.replace("]]>", "");
		//alert(doc);
		
		tabDoc = doc.split(',');
		html = '<table border="0">';
		//alert(fileName);
		
		//construction de la liste des fichiers
		for(i=0; i<tabDoc.length; i++)
		{
			idForUse = "fileId" + i;
			if(fileName==tabDoc[i])
			{
				lastSelectedFileId = idForUse;
			}
			html += '<tr><td id="'+ idForUse +'" class="fileItem" onmouseover="FileItemMouseOver(\''+ idForUse +'\')" onmouseout="FileItemMouseOut(\''+ idForUse +'\')" onclick="FileItemClick(\''+ tabDoc[i] +'\',\''+ idForUse + '\')">' + tabDoc[i] + '</td></tr>';
		}
		//alert(lastSelectedFileId);
		html += '</table>';
		//alert(html);
		
		document.all["folderContent"].innerHTML = html;
	}
	catch(e)
	{}
}

function SelectFolder(node)
{
	RealSelectFolder(node.Value);
}

function ClickOnFolder(selected)
{
	/*try
	{
		var Args;
		var tabSelected;
		var xmlHttp = null;
		var directory;
		
		var post;
		var response;
		var responseDocument;
		var doc;
		var tabDoc;
		var html;
				
		Args = window.dialogArguments;

		document.all["fileSelected"].value = "";
		
		tabSelected = selected.split(',');
		directory = tabSelected[tabSelected.length-1];
		//alert(directory);
		currentSelectedId = selected;
		currentSelected = document.all[directory].dataItem;
		
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp = null;
		}
		
		post = "directory=" + document.all[directory].dataItem + "&filter=" + document.all["choiceExts"].value;
		xmlHttp.Open("POST", Args.webServiceUrl + '/GetFilesFromFolder', false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.Send(post);
		
		doc = xmlHttp.responseXML.text;
			
		doc = doc.replace("<![CDATA[", "");
		doc = doc.replace("]]>", "");
		//alert(doc);
		
		tabDoc = doc.split(',');
		html = '<table border="0">';
		//alert(fileName);
		
		//construction de la liste des fichiers
		for(i=0; i<tabDoc.length; i++)
		{
			idForUse = "fileId" + i;
			if(fileName==tabDoc[i])
			{
				lastSelectedFileId = idForUse;
			}
			html += '<tr><td id="'+ idForUse +'" class="fileItem" onmouseover="FileItemMouseOver(\''+ idForUse +'\')" onmouseout="FileItemMouseOut(\''+ idForUse +'\')" onclick="FileItemClick(\''+ tabDoc[i] +'\',\''+ idForUse + '\')">' + tabDoc[i] + '</td></tr>';
		}
		//alert(lastSelectedFileId);
		html += '</table>';
		//alert(html);
		
		document.all["folderContent"].innerHTML = html;
	}
	catch(e)
	{}*/
}

function FileItemMouseOver(itemID)
{
	try
	{
		if(document.all[itemID] != undefined)
		{
			if(document.all[itemID].className == "fileItem")
			{
				document.all[itemID].className = "fileItemOver";
			}
		}
	}
	catch(e)
	{}
}

function FileItemMouseOut(itemID)
{
	try
	{
		if(document.all[itemID] != undefined)
		{
			if(document.all[itemID].className == "fileItemOver")
			{
				document.all[itemID].className = "fileItem";
			}
		}
	}
	catch(e)
	{}
}

function FileItemClick(itemLabel, idForUse)
{
	try
	{
		//alert("click sur : " + idForUse);
		//alert("last : " + lastSelectedFileId);
		
		if(lastSelectedFileId != null)
		{
			if(document.all[lastSelectedFileId] != null)
			{
				document.all[lastSelectedFileId].className = "fileItem";
			}
		}
		if (document.all[idForUse] != null)
		{
			document.all[idForUse].className = "fileSelected";	
			lastSelectedFileId = idForUse;
		}
		
		document.all["fileSelected"].value = itemLabel;
		
	}
	catch(e)
	{
		alert(e.err);
	}
}

function open()
{
	/*try
	{
		if(window.document.all["treeControl1rootSel"] != undefined && window.document.all["treeControl1rootSel"].tree != undefined)
		{
			var obj;
			var tmp;
			
			var i;
				
			obj = window.document.all[window.document.all["treeControl1rootSel"].tree.root.IdForExpand + "4"].firstChild;
			//alert("open de : " + window.document.all["treeControl1rootSel"].tree.root.tabMemFolders[0]);
			while(obj.dataItem != window.document.all["treeControl1rootSel"].tree.root.tabMemFolders[0] && obj != null)
			{
				//alert("objet : " + obj.dataItem + " tabMemFolder : " + window.document.all["treeControl1rootSel"].tree.root.tabMemFolders[0]);
				obj = obj.nextSibling;
			}
			if(obj != null)
			{
				//alert(obj.dataItem);
				window.document.all["treeControl1rootSel"].tree.root.IdForExpand = obj.id;
			}
			window.document.all[window.document.all["treeControl1rootSel"].tree.root.IdForExpand + "4"].style.display = "";
			//alert("expand de : " + window.document.all["treeControl1rootSel"].tree.root.IdForExpand);
			LoadData(window.document.all["treeControl1rootSel"].tree.root.IdForExpand);
			
			tmp = new Array();
			for(i=1; i<window.document.all["treeControl1rootSel"].tree.root.tabMemFolders.length; i++)
			{
				tmp[i-1] = window.document.all["treeControl1rootSel"].tree.root.tabMemFolders[i];
			}
			window.document.all["treeControl1rootSel"].tree.root.tabMemFolders = tmp;
			
			if(window.document.all["treeControl1rootSel"].tree.root.tabMemFolders.length != 0)
			{
				window.setTimeout("open()", 300);
			}
			else
			{
				//alert("click : " + window.document.all["treeControl1rootSel"].tree.root.IdForExpand);
				ClickOnFolder(window.document.all["treeControl1rootSel"].tree.root.IdForExpand);
				//alert("fichier : " + fileName);
				FileItemClick(fileName, lastSelectedFileId);
			}
		}
	}
	catch(e)
	{}*/
}

